//
// Created by livio on 2023/3/5.
//

#ifndef CPPDEMO_IOEVENT_H
#define CPPDEMO_IOEVENT_H

#include <memory>
#include <cstdint>

/**
 * definition the event of IO
 */
class IOEvent final : public std::enable_shared_from_this<IOEvent> {
public:
    enum class Event : uint8_t {
        READ = 0x01,
        WRITE = 0x02,
        RDWD = READ | WRITE,
    };
public:
    IOEvent() = default;

    explicit IOEvent(Event event);

    ~IOEvent() = default;

    // set readable
    inline void setRead(bool on) {
        on ? set(Event::READ) : clear(Event::READ);
    }

    // set writeable
    inline void setWrite(bool on) {
        on ? set(Event::WRITE) : clear(Event::WRITE);
    }

    // clear
    inline void clear() {
        m_event = 0;
    }

    // is readable
    inline bool isRead() const {
        return isSet(Event::READ);
    }

    // is writeable
    inline bool isWrite() const {
        return isSet(Event::WRITE);
    }

public:
    // is readable of the event
    static bool isRead(uint8_t event);

    // is writeable of the event
    static bool isWrite(uint8_t event);

protected:
    bool isSet(const Event &event) const;

    void set(const Event &event);

    void clear(const Event &event);

private:
    uint8_t m_event{0};
};


#endif //CPPDEMO_IOEVENT_H
